 <?php

class Jf_Acl extends Zend_Acl {
    /*
     * example_default: $roles = array('guest'=> '', 'editor' => "guest" ,'administrator' => 'editor') example_diy: $roles = array('guest' => '', 'simple' => '')
     */
    public function addRoles ($roles) {
        foreach ($roles as $name => $parents) {
            if ( ! $this -> hasRole ($name)) {
                if (empty ($parents)) {
                    $parents = null;
                } else {
                    $parents = explode (',', $parents);
                }
                $this -> addRole (new Zend_Acl_Role ($name), $parents);
            }
        }
        return $this;
    }
    /*
     * $resource = array('catalog_product' => 'catalog', 'catalog' => null)
     */
    public function addResources ($resources) {
        foreach ($resources as $resource => $parent) {
            if ( ! $this -> has ($resource)) {
                $this -> addResource ($resource, empty ($parent) ? null : $parent);
            }
        }
        return $this;
    }
    /*
     * $rules = array( 'TYPE_ALLOW' => array('catalog_product'=> array()) 'TYPE_DENY' => array('catalog_product'=> array()) )
     */
    public function setRulesByRole ($role, $rules) {
        $this -> addRole ($role);
        $types = array_intersect_assoc (array_keys ($rules), array(self::TYPE_ALLOW, self::TYPE_DENY));
        foreach ($types as $type) {
            $typeRules = $rules[$type];
            if (empty ($typeRules)) {
                continue;
            } elseif (count ($typeRules) === 1 && array_key_exists ('all', $typeRules)) {
                $typeRules = array_fill_keys ($this -> getResources (), null);
            }
            foreach ($typeRules as $resource => $privileges) {
                $this -> setRule (self::OP_ADD, $type, $role, $resource, empty ($privileges) ? null : $privileges, null);
            }
        }
        return $this;
    }
    /*
     * array( 'allow' => array('index' => array('all' => 'editor')) 'deny' => array('catalog_product' => array('all' => 'editor') )
     */
    public function setRules ($rules) {
        foreach ($rules as $type => $resources) {
            foreach ($resources as $resource => $privileges) {
                if ($resources == 'all') {
                    $resource = null;
                } else {
                    if ( ! $this -> has ($resource)) {
                        $this -> addResource ($resource);
                    }
                }
                foreach ($privileges as $privilege => $role) {
                    if ($privilege == 'all') {
                        $privilege = null;
                    }
                    if ($type == 'allow') {
                        $this -> allow ($role, $resource, $privilege);
                    }
                    if ($type == 'deny') {
                        $this -> deny ($role, $resource, $privilege);
                    }
                }
            }
        }
        return $this;
    }

}
